using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using Deployment.CustomTasks.Types;
using NAnt.Core;
using NAnt.Core.Attributes;

namespace Deployment.CustomTasks.Tasks
{
	[TaskName("wizard")]
	public class WizardTask : Task
	{
		private string title;
		private string propertyPrefix;
		private WizardScreenElement[] screenElements;
		private TaskContainer _doStuff;

		[BuildElementArray("screen", ElementType = typeof(WizardScreenElement))]
		public WizardScreenElement[] ScreenElements
		{
			get { return screenElements; }
			set { screenElements = value; }
		}

		[TaskAttribute("title", Required = true)]
		public string Title
		{
			get
			{
				
				return title;
			}
			set { title = value; }
		}

		[TaskAttribute("propertyPrefix", Required = true)]
		public string PropertyPrefix
		{
			get { return propertyPrefix; }
			set { propertyPrefix = value; }
		}

		[BuildElement("do")]
		public TaskContainer StuffToDo
		{
			get { return _doStuff; }
			set { _doStuff = value; }
		}


		///<summary>
		///Executes the task.
		///</summary>
		///
		protected override void ExecuteTask()
		{
			foreach (WizardScreenElement screenElement in screenElements)
			{
				screenElement.WizardName = this.Title;
				Application.Run(screenElement.GetForm());
				if (screenElement.Cancel)
				{
					Log(Level.Warning, "User clicked cancel.  Cancelling wizard.");
					return;
				}
				foreach (WizardScreenItemElement screenItemElement in screenElement.ItemElements)
				{
					string propertyName = PropertyPrefix + "." + screenItemElement.Id;
					Project.Properties[propertyName] = screenItemElement.Value;
				}
			}
//			StuffToDo.Execute();
		}
	}
}
